package com.example.dllo.exe.giftspeak.songnvpiao;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dllo.exe.bean.SongNvPiaoBean;
import com.example.dllo.exe.R;
import com.example.dllo.exe.fengzhuang.DBTool;
import com.example.dllo.exe.me.MyUser;
import com.example.dllo.exe.me.ZhuCeAty;
import com.example.dllo.exe.bean.SQLGongLueBean;
import com.squareup.picasso.Picasso;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * code is far away from bug with the animal protecting
 * <p/>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class SongNvPiaoAdapter extends BaseAdapter {
    private SongNvPiaoBean songNvPiaoBean;
    private Context context;
    private ItemOnclick onclick;
    Bitmap bitmap;

    public void setOnclick(ItemOnclick onclick) {
        this.onclick = onclick;
    }

    public SongNvPiaoAdapter(Context context) {
        this.context = context;

    }

    public void setSongNvPiaoBean(SongNvPiaoBean songNvPiaoBean) {
        this.songNvPiaoBean = songNvPiaoBean;

        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return songNvPiaoBean != null ? songNvPiaoBean.getData().getItems().size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {

        ViewHolder viewHolder = null;
        LayoutInflater inflater = LayoutInflater.from(context);
        if (view == null) {
            view = inflater.inflate(R.layout.songnvpiaofragmentlayout, null);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        try {
            viewHolder.descriptiontext.setText(songNvPiaoBean.getData().getItems().get(i).getTitle());
            viewHolder.categorytext.setText(songNvPiaoBean.getData().getItems().get(i).getColumn().getCategory());
            viewHolder.titletexView.setText(songNvPiaoBean.getData().getItems().get(i).getColumn().getTitle());
            viewHolder.likesTextView.setText(songNvPiaoBean.getData().getItems().get(i).getLikes_count() + "");
            viewHolder.nicknameText.setText(songNvPiaoBean.getData().getItems().get(i).getAuthor().getNickname());
            Picasso.with(context).load(songNvPiaoBean.getData().getItems().get(i).getCover_image_url()).into(viewHolder.imageImagview);
            Picasso.with(context).load(songNvPiaoBean.getData().getItems().get(i).getAuthor().getAvatar_url()).into(viewHolder.authorImage);

            viewHolder.imageImagview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onclick.onItemClick(i);
                }
            });
            final ViewHolder finalViewHolder = viewHolder;

            viewHolder.imageViewxinxing.setOnClickListener(new View.OnClickListener() {

                private SQLGongLueBean sqlGongLueBean;



                @Override
                public void onClick(View view) {
                    MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
                    if (myUser != null) {
                        sqlGongLueBean = new SQLGongLueBean();



                        sqlGongLueBean.setName(songNvPiaoBean.getData().getItems().get(i).getAuthor().getNickname());
                        sqlGongLueBean.setTitle(songNvPiaoBean.getData().getItems().get(i).getTitle());
                        sqlGongLueBean.setUrl(songNvPiaoBean.getData().getItems().get(i).getCover_image_url());
                        DBTool.getInstance().insertStudent(sqlGongLueBean);



                        ScaleAnimation sa = new ScaleAnimation(0, 1.0f, 0, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                        sa.setDuration(2000);
                        sa.setRepeatCount(0);
                        finalViewHolder.imageViewxinxing.setImageResource(R.mipmap.muchlike);
                        finalViewHolder.imageViewxinxing.startAnimation(sa);
                        Toast toast=new Toast(context.getApplicationContext());
                        View viewtoast=LayoutInflater.from(context).inflate(R.layout.toast_layout,null);
                        toast.setView(viewtoast);
                        toast.setGravity(Gravity.CENTER,0,0);
                        toast.show();


                        Collection collection = new Collection();
                        collection.setName(
                                songNvPiaoBean.getData().getItems().get(i).getAuthor().getNickname());
                        collection.setTitle(songNvPiaoBean.getData().getItems().get(i).getTitle());
                        collection.setUrl(songNvPiaoBean.getData().getItems().get(i).getCover_image_url());

//N是
                        collection.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null) {
                                    Log.d("SongNvPiaoAdapter", "有数据");
                                } else {
                                    Log.d("SongNvPiaoAdapter", "没有数据");
                                }
                            }
                        });
                        Intent intent=new Intent(context,SongNvPiaoSecondTopAty.class);
                        intent.putExtra("key",songNvPiaoBean.getData().getItems().get(i).getUrl());

//                       context startActivity(intent);

                    } else {
                        Intent intent = new Intent(context.getApplicationContext(), ZhuCeAty.class);
                        context.startActivity(intent);
                    }


                }
            });

        } catch (NullPointerException e) {
            Log.d("SongNvPiaoAdapter", "没有数据" + i);

        }
        ObjectAnimator.ofFloat(view,"translationY",400,0).setDuration(1000).start();
        ObjectAnimator.ofFloat(view,"rotationX",360,0).setDuration(1000).start();
        return view;
    }

    public interface ItemOnclick {
        void onItemClick(int pos);
    }

    public class ViewHolder {
        TextView titletexView;
        ImageView imageImagview;
        TextView likesTextView;
        TextView categorytext;
        ImageView authorImage;
        TextView nicknameText;
        TextView descriptiontext;
        ImageView imageViewxinxing;

        public ViewHolder(View view) {
            titletexView = (TextView) view.findViewById(R.id.title_text_songnvpiao);
            imageImagview = (ImageView) view.findViewById(R.id.songnvpiao_ImageImageView);
            likesTextView = (TextView) view.findViewById(R.id.songnvpiao_likescountTextView);
            categorytext = (TextView) view.findViewById(R.id.category_text);
            descriptiontext = (TextView) view.findViewById(R.id.songnvpiao_descriptionTextView);
            authorImage = (ImageView) view.findViewById(R.id.avatar_image);
            nicknameText = (TextView) view.findViewById(R.id.nickname_text);
            imageViewxinxing = (ImageView) view.findViewById(R.id.image_xinxing);
        }
    }
}
